package com.rd.student.jiaji.class2.duyukun.Day0724;

//import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;

import java.util.Random;
import java.util.concurrent.*;

public class TestCyclicBarrier {
    public static void main(String[] args) {
        ExecutorService service = Executors.newCachedThreadPool();
        CyclicBarrier cyclicBarrier = new CyclicBarrier(5, new Runnable() {
            @Override
            public void run() {

                System.out.println("全部到达可以点餐");

                service.shutdown();
            }
        });
        for (int i = 0; i < 5; i++) {
            service.execute(new Runnable() {
                @Override
                public void run() {
                    try{
                        Thread.sleep(new Random().nextInt(2000));
                        System.out.println(Thread.currentThread().getName()
                                + "到饭店");

                        cyclicBarrier.await();
                    }catch (InterruptedException e){

                        throw new RuntimeException(e);

                    }catch (BrokenBarrierException e){

                        throw new RuntimeException(e);
                    }
                }
            });
        }
    }
}
